package com.qingcloud.task.controller;

import com.qingcloud.task.dto.TaskListDTO;
import com.qingcloud.task.dto.TaskSaveDTO;
import com.qingcloud.task.service.TaskService;
import com.qingcloud.task.util.JsonResult;
import com.qingcloud.task.vo.PageVO;
import com.qingcloud.task.vo.TaskInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhengjibin
 * @version 1.0.0
 * @ClassName TaskController.java
 * @Description
 * @createTime 2022年01月24日 13:51:00
 */
@Api(value = "任务管理", tags = "任务管理")
@RequestMapping(value = "/task")
@RestController
public class TaskController {
    @Autowired
    private TaskService taskService;

    @ApiOperation("查询任务基本信息")
    @GetMapping(value = "/taskInfo")
    public JsonResult taskInfo(@RequestParam(value = "taskId") Long taskId){
        TaskInfoVO taskInfo = taskService.getTaskInfo(taskId);
        return JsonResult.success(taskInfo);
    }

    @ApiOperation("保存任务")
    @PostMapping(value = "/saveTask")
    public JsonResult saveTask(@RequestBody TaskSaveDTO dto){
        Long taskId = taskService.saveTask(dto);
        return JsonResult.success(taskId);
    }

    @ApiOperation("分页查询")
    @PostMapping(value = "/selectPage")
    public JsonResult selectPage(@RequestBody TaskListDTO dto){
        PageVO<TaskInfoVO> pageVO = taskService.selectPage(dto);
        return JsonResult.success(pageVO);
    }

    @ApiOperation("修改任务")
    @PostMapping(value = "/updateTask")
    public JsonResult updateTask(@RequestBody TaskInfoVO dto){
        taskService.updateTask(dto);
        return JsonResult.success();
    }
    @ApiOperation("任务转派")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId" ,value = "任务id",dataTypeClass = String.class),
            @ApiImplicitParam(name = "userId",value = "历史执行人id",dataTypeClass = String.class),
            @ApiImplicitParam(name = "toUserId",value = "新执行人id",dataTypeClass = String.class)
    })
    @PostMapping(value = "/taskTransfer")
    public JsonResult taskTransfer(@RequestParam(value = "taskId") Long taskId,
                                   @RequestParam(value = "userId") String userId,
                                   @RequestParam(value = "toUserId") String toUserId){
        taskService.taskTransfer(taskId,userId,toUserId);
        return JsonResult.success();
    }

    @ApiOperation(value = "任务接收",notes = "任务接收后，将进入开始状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId" ,value = "任务id",dataTypeClass = String.class),
            @ApiImplicitParam(name = "userId",value = "执行人id",dataTypeClass = String.class)
    })
    @PostMapping(value = "/receive")
    public JsonResult receive(@RequestParam(value = "taskId") Long taskId,@RequestParam(value = "userId") String userId){
        taskService.receive(taskId,userId);
        return JsonResult.success();
    }

    @ApiOperation(value = "任务完成",notes = "任务接收后，将进入开始状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId" ,value = "任务id",dataTypeClass = String.class),
            @ApiImplicitParam(name = "userId",value = "执行人id",dataTypeClass = String.class)
    })
    @PostMapping(value = "/complete")
    public JsonResult complete(@RequestParam(value = "taskId") Long taskId,@RequestParam(value = "userId") String userId){
        taskService.complete(taskId,userId);
        return JsonResult.success();
    }
}
